package poj3280;

import java.util.Scanner;

public class Main
{
	private static Scanner sc = new Scanner(System.in);
	
	public static void main(String[] args)
	{
		int m = sc.nextInt();
		int n = sc.nextInt();
		
		char[] str = sc.next().toCharArray();
		
		int[] ab = new int[128];
		for(int i=0; i<m; i++)
		{
			int index = sc.next().toCharArray()[0];
			int n1 = sc.nextInt();
			int n2 = sc.nextInt();
			
			ab[index] = n1 > n2 ? n2 : n1;
		}
		
		int nums[][] = new int[n][n];
		
		for(int j=0; j<n-1; j++)
		{
			if(str[j] == str[j+1])
				nums[1][j] = 0;
			else
				nums[1][j] = ab[str[j]] > ab[str[j+1]] ? ab[str[j+1]] : ab[str[j]];
		}
		
		for(int i=2; i<n; i++)
		{
			int en = n-i;
			for(int j=0; j<en; j++)
			{
				if(str[j] == str[j+i])
				{
					nums[i][j] = nums[i-2][j+1];
				}
				else
				{
					int a1 = ab[str[j]] + nums[i-1][j+1];
					int a2 = ab[str[j+i]] + nums[i-1][j];
					
					nums[i][j] = a1 > a2 ? a2 : a1;
				}
			}
		}
		
		System.out.println(nums[n-1][0]);
	}
}
